.ad l
.TH ED 1
.CT 1 editor
.ds . (\|\f5.\fP\|)
.ds .. (\|\f5.,.\fP\|)
.ds 1$ (\|\f51,$\fP\|)
.SH NAME
ed, e \- text editor
.SH SYNOPSIS
.B ed
[
.B -
]
[
.B -o
]
[
.I file
]
.SH DESCRIPTION
.I Ed
is the standard text editor;
.I e
is another name for it.
.PP
If a
.I file
argument is given,
.I ed
simulates an
.B e
command (see below) on that file:
it is read into
.I ed's
buffer so that it can be edited.
The options are
.TP
.B -
Suppress the printing
of character counts by
.BR e ,
.BR r ,
and
.B w
commands and of the confirming 
.B !
by
.B !
commands.
.TP
.B -o
(for output piping)
Place on the standard error file all output except writing by
.B w
commands.
If no 
.I file
is given, make
.I /dev/stdout
the remembered file; see the
.B e
command below. 
.I Ed
starts in append mode (simulating an
.B a
command).
.PP
.I Ed
operates on a `buffer', a copy of the file it is editing;
changes made
in the buffer have no effect on the file until a
.B w
(write)
command is given.
The copy of the text being edited resides
in a temporary file called the 
.IR buffer .
.PP
Commands to
.I ed
have a simple and regular structure: zero or
more
.I addresses
followed by a single character
.I command,
possibly
followed by parameters to the command.
These addresses specify one or more lines in the buffer.
Missing addresses are supplied by default.
.PP
In general, only one command may appear on a line.
Certain commands allow the 
addition of text to the buffer.
While
.I ed
is accepting text, it is said
to be in
.I  "input mode."
In this mode, no commands are recognized;
all input is merely collected.
Input mode is left by typing a period 
.B .
alone at the
beginning of a line.
.PP
.I Ed
supports a limited form of
.I "regular expression"
notation.
A regular expression specifies
a set of strings of characters.
A member of this set of strings is said to be
.I matched
by the regular expression.
In the following specification for regular expressions
the word `character' means any character but newline.
.IP 1.
Any character except a special character
matches itself.
Special characters are
the regular expression delimiter plus
.B \e[.
and sometimes
.BR ^*$ .
.IP 2.
A
.B .
matches any character.
.IP 3.
A
.B \e
followed by any character except a digit,
.BR ( ,
or
.B )
matches that character.
.IP 4.
A nonempty string
.I s
bracketed
.BI [ \|s\| ]
(or
.BI [^ s\| ])
matches any character in (or not in)
.I s.
In 
.I s,
.B \e
has no special meaning, and
.B ]
may only appear as
the first letter.
A substring 
.IB a - b ,
with
.I a
and
.I b
in ascending
.SM Unicode 
order, stands for the inclusive
range of
.SM Unicode 
characters.
.IP 5.
A regular expression of form 1-4 followed by
.B *
matches a sequence of
0 or more matches of the regular expression.
.IP 6.
A regular expression,
.I x,
of form 1-8, bracketed
.BI \e( \|x\| \e)
matches what
.I x
matches.
.IP 7.
A
.B \e
followed by a digit 
.I n
matches a copy of the string that the
bracketed regular expression beginning with the
.IR n th
.B \e(
matched.
.IP 8.
A regular expression of form 1-8,
.I x,
followed by a regular expression of form 1-7,
.I y
matches a match for
.I x
followed by a match for
.I y,
with the
.I x
match being as long as possible while still permitting a
.I y
match.
.IP 9.
A regular expression of form 1-8, or a null string, preceded by
.B ^
(and/or followed by
.BR $ ),
is constrained to matches that
begin at the left (and/or end at the right) end of a line.
.IP 10.
A regular expression of form 1-9 picks out the
longest among the leftmost matches in a line.
.IP 11.
An empty regular expression stands for a copy of the
last regular expression encountered.
.PP
Regular expressions are used in addresses to specify
lines and in one command
(see
.I s
below)
to specify a portion of a line which is to be replaced.
If it is desired to use one of
the regular expression metacharacters as an ordinary
character, that character may be preceded by `\e'.
This also applies to the character bounding the regular
expression (often 
.BR / )
and to
.B \e
itself.
.PP
To understand addressing in
.I ed
it is necessary to know that at any time there is a
.I "current line."
Generally speaking, the current line is
the last line affected by a command; however,
the exact effect on the current line
is discussed under the description of
the command.
Addresses are constructed as follows.
.TP
1.
The character
.BR . ,
customarily called `dot',
addresses the current line.
.TP
2.
The character 
.B $
addresses the last line of the buffer.
.TP
3.
A decimal number
.I n
addresses the
.IR n -th
line of the buffer.
.TP
4.
.BI \'x
addresses the line marked with the name
.IR x ,
which must be a lower-case letter.
Lines are marked with the
.B k
command described below.
.TP
5.
A regular expression enclosed in slashes 
.B /
addresses
the line found by searching forward from the current line
and stopping at the first line containing a
string that matches the regular expression.
If necessary the search wraps around to the beginning of the
buffer.
.TP
6.
A regular expression enclosed in queries 
.B ?
addresses
the line found by searching backward from the current line
and stopping at the first line containing
a string that matches the regular expression.
If necessary
the search wraps around to the end of the buffer.
.TP
7.
An address followed by a plus sign 
.B +
or a minus sign
.B -
followed by a decimal number specifies that address plus
(resp. minus) the indicated number of lines.
The plus sign may be omitted.
.TP
8.
An address followed by 
.B +
(or
.BR - )
followed by a
regular expression enclosed in slashes specifies the first
matching line following (or preceding) that address.
The search wraps around if necessary.
The 
.B +
may be omitted, so
.B 0/x/
addresses the
.I first
line in the buffer with an 
.BR x .
Enclosing the regular expression in 
.B ?
reverses the search direction.
.TP
9.
If an address begins with 
.B +
or
.B -
the addition or subtraction is taken with respect to the current line;
e.g.\&
.B -5
is understood to mean
.BR .-5 .
.TP
10.
If an address ends with 
.B +
or
.BR - ,
then 1 is added (resp. subtracted).
As a consequence of this rule and rule 9,
the address
.B -
refers to the line before the current line.
Moreover,
trailing
.B +
and
.B -
characters
have cumulative effect, so
.B --
refers to the current
line less 2.
.TP
11.
To maintain compatibility with earlier versions of the editor,
the character 
.B ^
in addresses is 
equivalent to
.BR - .
.PP
Commands may require zero, one, or two addresses.
Commands which require no addresses regard the presence
of an address as an error.
Commands which accept one or two addresses
assume default addresses when insufficient are given.
If more addresses are given than such a command requires,
the last one or two (depending on what is accepted) are used.
.PP
Addresses are separated from each other typically by a comma
.BR , .
They may also be separated by a semicolon
.BR ; .
In this case the current line 
is set to
the previous address before the next address is interpreted.
If no address precedes a comma or semicolon, line 1 is assumed;
if no address follows, the last line of the buffer is assumed.
The second address of any two-address sequence
must correspond to a line following the line corresponding to the first address.
.PP
In the following list of
.I ed
commands, the default addresses
are shown in parentheses.
The parentheses are not part of
the address, but are used to show that the given addresses are
the default.
`Dot' means the current line.
.TP
.RB \*. \|a
.br
.ns
.TP
<text>
.br
.ns
.TP
.B .
Read the given text
and append it after the addressed line.
Dot is left
on the last line input, if there
were any, otherwise at the addressed line.
Address 
.B 0
is legal for this command; text is placed
at the beginning of the buffer.
.TP
.RB \*(.. \|b [ +- ][\fIpagesize\fP][ pln\fR]
Browse.
Print a `page', normally 20 lines.
The optional 
.B +
(default) or
.B -
specifies whether the next or previous
page is to be printed.
The optional
.I pagesize
is the number of lines in a page.
The optional
.BR p ,
.BR n ,
or 
.B l
causes printing in the specified format, initially
.BR p .
Pagesize and format are remembered between 
.B b
commands.
Dot is left at the last line displayed.
.TP
.RB \*(.. \|c
.br
.ns
.TP
<text>
.br
.ns
.TP
.B .
.br
Change.
Delete the addressed lines, then accept input
text to replace these lines.
Dot is left at the last line input; if there were none,
it is left at the line preceding the deleted lines.
.TP
.RB \*(.. \|d
Delete the addressed lines from the buffer.
Dot is set to the line following the last line deleted, or to
the last line of the buffer if the deleted lines had no successor.
.TP
.BI e " filename"
Edit.
Delete the entire contents of the buffer;
then read the named file into the buffer.
Dot is set to the last line of the buffer.
The number of characters read is typed.
The file name is remembered for possible use in later
.BR e ,
.BR r ,
or
.B w
commands.
If
.I filename
is missing, the remembered name is used.
.TP
.BI E " filename"
Unconditional
.BR e ;
see `DIAGNOSTICS' below.
.TP
.BI f " filename"
Print the currently remembered file name.
If
.I filename
is given,
the currently remembered file name is first changed to
.I filename.
.TP
.RB \*(1$ \|g/\fIregular\ expression\fP/\fIcommand\ list\fP
.PD 0
.TP
.RB \*(1$ \|g/\fIregular\ expression\fP/
.TP
.RB \*(1$ \|g/\fIregular\ expression\fP
.PD
Global.
First mark every line which matches
the given
.I regular expression.
Then for every such line, execute the
.I command list
with dot initially set to that line.
A single command or the first of multiple commands
appears on the same line with the global command.
All lines of a multi-line list except the last line must end with
.BR \e .
The
.RB \&` \&. \&'
terminating input mode for an
.BR a ,
.BR i ,
.B c
command may be omitted if it would be on the
last line of the command list.
The commands
.B g
and
.B v
are not permitted in the command list.
Any character other than space or newline may
be used instead of 
.B /
to delimit the regular expression.
The second and third forms mean
.BI g/ regular\ expression /p .
.TP
.RB (\| .\| ) \|i
.PD 0
.TP
<text>
.TP
.B .
Insert the given text before the addressed line.
Dot is left at the last line input, or, if there were none,
at the line before the addressed line.
This command differs from the
.I a
command only in the placement of the
text.
.PD
.TP
.RB (\| .,.+1 \|) \|j
Join the addressed lines into a single line;
intermediate newlines are deleted.
Dot is left at the resulting line.
.TP
.RB \*. \|k\fIx\fP
Mark the addressed line with name
.I x,
which must be a lower-case letter.
The address form
.BI \' x
then addresses this line.
.ne 2.5
.TP
.RB \*(.. \|l
List.
Print the addressed lines in an unambiguous way:
a tab is printed as
.BR \et ,
a backspace as
.BR \eb ,
backslashes as
.BR \e\e ,
and printable ASCII characters are printed as themselves.
All other (Uncicode) characters are printed as a 
backslash and three octal digits for each byte of their UTF-8
representation.
Long lines are folded,
with the second and subsequent sub-lines indented one tab stop.
If the last character in the line is a blank,
it is followed by
.BR \en .
An
.B l
may be appended, like
.BR p ,
to any non-I/O command.
.TP
.RB \*(.. \|m\fIa
Move.
Reposition the addressed lines after the line
addressed by
.IR a .
Dot is left at the last moved line.
.TP
.RB \*(.. \|n
Number.
Perform
.BR p ,
prefixing each line with its line number and a tab.
An
.B n
may be appended, like
.BR p ,
to any non-I/O command.
.TP
.RB \*(.. \|p
Print the addressed lines.
Dot is left at the last line printed.
A
.B p
appended to any non-I/O command causes the then current line
to be printed after the command is executed.
.TP
.RB \*(.. \|P
This command is a synonym for
.BR p .
.TP
.B q
Quit the editor.
No automatic write
of a file is done.
.TP
.B Q
Quit unconditionally; see `DIAGNOSTICS' below.
.TP
.RB ( $ )\|r\ \fIfilename\fP
Read in the given file after the addressed line.
If no
.I filename
is given, the remembered file name is used.
The file name is remembered if there were no
remembered file name already.
If the read is successful, the number of characters
read is typed.
Dot is left at the last line read in from the file.
.TP
.RB \*(.. \|s\fIn\fP/\fIregular\ expression\fP/\fIreplacement\fP/
.PD 0
.TP
.RB \*(.. \|s\fIn\fP/\fIregular\ expression\fP/\fIreplacement\fP/g
.TP
.RB \*(.. \|s\fIn\fP/\fIregular\ expression\fP/\fIreplacement\fP
.PD
Substitute.
Search each addressed
line for an occurrence of the specified regular expression.
On each line in which
.I n
matches are found
.RI ( n
defaults to 1 if missing),
the
.IR n th
matched string is replaced by the replacement specified.
If the global replacement indicator 
.B g
appears after the command,
all subsequent matches on the line are also replaced.
It is an error for the substitution to fail on all addressed lines.
Any character other than space or newline
may be used instead of 
.B /
to delimit the regular expression
and the replacement.
Dot is left at the last line substituted.
The third form means
.BI s n / regular\ expression / replacement\fP/p\fR.
The second
.B /
may be omitted if the replacement is
empty.
.IP
An ampersand 
.B &
appearing in the replacement
is replaced by the string matching the regular expression.
The characters
.BI \e n,
where
.I n
is a digit,
are replaced by the text matched by the
.IR n -th
regular subexpression
enclosed between
.B \e(
and
.BR \e) .
When
nested, parenthesized subexpressions
are present,
.I n
is determined by counting occurrences of
.BR \e (
starting from the left.
.IP
A literal 
.BR & ,
.BR / ,
.B \e
or newline may be included in a replacement
by prefixing it with
.BR \e .
.TP
.RB \*(.. \|t\|\fIa
Transfer.
Copy the addressed lines 
after the line addressed by
.I a.
Dot is left at the last line of the copy.
.TP
.RB \*(.. \|u
Undo.
Restore the preceding contents
of the current line, which must be the last line
in which a substitution was made.
.TP
.RB \*(1$ \|v/\fIregular\ expression\fP/\fIcommand\ list\fP
.PD 0
.TP
.RB \*(1$ \|v/\fIregular\ expression\fP/
.TP
.RB \*(1$ \|v/\fIregular\ expression\fP
.PD
This command is the same as the global command
.B g
except that the command list is executed with
dot initially set to every line
.I except
those
matching the regular expression.
.TP
.RB \*(1$ \|w " \fIfilename\fP"
Write the addressed lines onto
the given file.
If the file does not exist,
it is created with mode 666 (readable and writable by everyone).
If no
.I filename
is given, the remembered file name, if any, is used.
The file name is remembered if there were no 
remembered file name already.
Dot is unchanged.
If the write is successful, the number of characters written is
printed.
.TP
.RB \*(1$ \|W " \fIfilename\fP"
Perform
.BR w ,
but append to, instead of overwriting, any existing file contents.
.TP
.RB ( $ ) \|=
Print the line number of the addressed line.
Dot is unchanged.
.TP
.BI ! shell\ command
Send the remainder of the line after the 
.B !
to
.IR sh (1)
to be interpreted as a command.
Dot is unchanged.
.TP
.RB (\| .+1 )\|<newline>
An address without a command is taken as a
.B p 
command.
A terminal
.B /
may be omitted from the address.
A blank line alone is equivalent to
.BR .+1p ;
it is useful
for stepping through text.
.PP
If an interrupt signal 
.SM (ASCII DEL)
is sent,
.I ed
prints a 
.B ?
and returns to its command level.
.PP
When reading a file,
.I ed
discards
.SM ASCII NUL
characters. If there are any characters
after the last newline,
.I ed
supplies a final newline and prints the
message
.I ?N appended
on the terminal.
It refuses to read files not conforming to the
.SM UTF-8
standard.
.SH FILES
.B /tmp/e*
.br
.B ed.hup
\ \ work is saved here if terminal hangs up
.SH "SEE ALSO"
.IR qed (1), 
.IR sed (1), 
.IR vi (1)
.SH DIAGNOSTICS
.BI ? name
for inaccessible file;
.B ?TMP
for temporary file overflow;
.B ?N appended
when a final newline has been appended;
.B ?
for errors in commands or other overflows.
.PP
To protect against throwing away valuable work,
a
.B q
or
.B e
command is considered to be in error if the buffer has
been modified since the last
.BR w ,
.BR q ,
or
.B e
command.
